package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyCommunityDoorAddBo;
import com.ruoyi.system.bo.ZsyyCommunityDoorQueryBo;
import com.ruoyi.system.bo.ZsyyCommunityDoorEditBo;
import com.ruoyi.system.domain.ZsyyCommunityDoor;
import com.ruoyi.system.mapper.ZsyyCommunityDoorMapper;
import com.ruoyi.system.vo.ZsyyCommunityDoorVo;
import com.ruoyi.system.service.IZsyyCommunityDoorService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 门禁状态管理Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyCommunityDoorServiceImpl extends ServicePlusImpl<ZsyyCommunityDoorMapper, ZsyyCommunityDoor> implements IZsyyCommunityDoorService {
    @Autowired
    private ZsyyCommunityDoorMapper iZsyyCommunityDoorMapper;
	@Autowired
	private GetCommunityIdImpl getCommunityIdImpl;

    @Override
    public ZsyyCommunityDoorVo queryById(Long id){
        return getVoById(id, ZsyyCommunityDoorVo.class);
    }

    @Override
    public TableDataInfo<ZsyyCommunityDoorVo> queryPageList(ZsyyCommunityDoorQueryBo bo) {
		bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
		Page<ZsyyCommunityDoorVo> result = iZsyyCommunityDoorMapper.selectListByQuery(PageUtils.buildPage(), bo);
		return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZsyyCommunityDoorVo> queryList(ZsyyCommunityDoorQueryBo bo) {
		bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
		return iZsyyCommunityDoorMapper.queryListByExport(bo);
    }

    private LambdaQueryWrapper<ZsyyCommunityDoor> buildQueryWrapper(ZsyyCommunityDoorQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyCommunityDoor> lqw = Wrappers.lambdaQuery();
		bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
        lqw.like(StrUtil.isNotBlank(bo.getDeviceName()), ZsyyCommunityDoor::getDeviceName, bo.getDeviceName());
        lqw.eq(StrUtil.isNotBlank(bo.getDeviceId()), ZsyyCommunityDoor::getDeviceId, bo.getDeviceId());
        lqw.eq(bo.getDeviceType() != null, ZsyyCommunityDoor::getDeviceType, bo.getDeviceType());
        lqw.eq(StrUtil.isNotBlank(bo.getDeviceIp()), ZsyyCommunityDoor::getDeviceIp, bo.getDeviceIp());
        lqw.eq(StrUtil.isNotBlank(bo.getPort()), ZsyyCommunityDoor::getPort, bo.getPort());
        lqw.like(StrUtil.isNotBlank(bo.getUserName()), ZsyyCommunityDoor::getUserName, bo.getUserName());
        lqw.eq(StrUtil.isNotBlank(bo.getDeviceNo()), ZsyyCommunityDoor::getDeviceNo, bo.getDeviceNo());
        lqw.eq(bo.getPowerType() != null, ZsyyCommunityDoor::getPowerType, bo.getPowerType());
        lqw.eq(bo.getOnLine() != null, ZsyyCommunityDoor::getOnLine, bo.getOnLine());
        lqw.eq(bo.getDeviceStatus() != null, ZsyyCommunityDoor::getDeviceStatus, bo.getDeviceStatus());
        lqw.eq(bo.getCameraId() != null, ZsyyCommunityDoor::getCameraId, bo.getCameraId());
        lqw.eq(bo.getUnitId() != null, ZsyyCommunityDoor::getUnitId, bo.getUnitId());
        lqw.eq(bo.getCommunityId() != null, ZsyyCommunityDoor::getCommunityId, bo.getCommunityId());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyCommunityDoorAddBo bo) {
        ZsyyCommunityDoor add = BeanUtil.toBean(bo, ZsyyCommunityDoor.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyCommunityDoorEditBo bo) {
        ZsyyCommunityDoor update = BeanUtil.toBean(bo, ZsyyCommunityDoor.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyCommunityDoor entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
